package org.ljy.rabbitextensionsdemo.config;


import org.ljy.rabbitextensionsdemo.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DlConfig {

//    //正常的交换机和队列
//    @Bean("normalQueue")
//    public Queue normalQueue() {
//        return QueueBuilder.durable(Constant.NORMAL_QUEUE)
//                .deadLetterExchange(Constant.DL_EXCHANGE)//绑定死信交换机
//                .deadLetterRoutingKey("dlx")//死信交换机的路由规则
////                .ttl(10000)//10秒后消息过期
//                .maxLength(10L)//队列最大长度为10
//                .build();
//    }


    //正常的交换机和队列
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constant.NORMAL_QUEUE)
                .deadLetterExchange(Constant.DL_EXCHANGE)//绑定死信交换机
                .deadLetterRoutingKey("dlx")//死信交换机的路由规则
                .build();
    }

    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constant.NORMAL_EXCHANGE).build();
    }

    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange") DirectExchange normalExchange, @Qualifier("normalQueue")Queue normalQueue) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("normal");
    }

    //死信交换机和队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(Constant.DL_QUEUE).build();
    }

    @Bean("dlxExchange")
    public DirectExchange dlxExchange() {
        return ExchangeBuilder.directExchange(Constant.DL_EXCHANGE).build();
    }

    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxExchange") DirectExchange dlxExchange, @Qualifier("dlxQueue")Queue dlxQueue) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("dlx");
    }


    //事务
    @Bean("transQueue")
    public Queue transQueue() {
        return QueueBuilder.durable(Constant.TRANS_QUEUE).build();
    }


}
